package com.mallcloud.mall.app.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mallcloud.mall.app.api.entity.dto.AppProductSearchDTO;
import com.mallcloud.mall.app.api.entity.vo.*;
import com.mallcloud.mall.app.config.BaseConfig;
import com.mallcloud.mall.app.service.AppProductService;
import com.mallcloud.mall.converter.SpuConverter;
import com.mallcloud.mall.product.api.entity.SpuModel;
import com.mallcloud.mall.product.api.vo.AppSpuVO;
import com.mallcloud.mall.product.mapper.ShopDetailMapper;
import com.mallcloud.mall.product.mapper.SpuMapper;
import com.mallcloud.mall.product.service.ShopDetailService;
import com.mallcloud.mall.product.service.SkuService;
import com.mallcloud.mall.product.service.SpuService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品分类
 */
@Slf4j
@Service
@AllArgsConstructor
public class AppProductServiceImpl extends ServiceImpl<SpuMapper, SpuModel> implements AppProductService {

    private final BaseConfig baseConfig;
    private final ShopDetailService shopDetailService;
    private final ShopDetailMapper shopDetailMapper;
    private final SpuMapper spuMapper;

    @Override
    public Page<AppProductSearchShopVO> pageSearchShop(Page page, AppProductSearchDTO dto) {
        // 分页查询包含此分类商品的店铺
        IPage<Long> shopIds = shopDetailMapper.pageShopDetailGroup(page, dto.getCategoryId());

        List<AppProductSearchShopVO> productCommons = spuMapper.listSpuGroup(dto.getCategoryId(), shopIds.getRecords());
        return Page.<AppProductSearchShopVO>of(shopIds.getCurrent(), shopIds.getSize(), shopIds.getTotal())
                .setRecords(productCommons);
    }

    @Override
    public Page<AppProductSearchSpuVO> pageSearchSpu(Page page, AppProductSearchDTO dto) {
        return spuMapper.pageSearchSpu(page, dto);
    }
//
//    @Override
//    public Page<SpuModel> pageProduct(Page<SpuModel> page, AppProductSearchDTO dto) {
//        LambdaQueryWrapper<SpuModel> queryWrap = Wrappers.<SpuModel>lambdaQuery()
//                .select(SpuModel::getId,
//                        SpuModel::getShopId,
//                        SpuModel::getImage,
//                        SpuModel::getName,
//                        SpuModel::getUnitName,
//                        SpuModel::getPrice,
//                        SpuModel::getOtPrice,
//                        SpuModel::getSales,
//                        SpuModel::getFicti,
//                        SpuModel::getCategoryId,
//                        SpuModel::getBrandId
//                )
//                .eq(SpuModel::getIsShow, true)
//                .in(SpuModel::getAuditStatus, ProductEnum.AuditEnum.SUCCESS.getCode(), ProductEnum.AuditEnum.NO_AUDIT.getCode())
//                .gt(SpuModel::getStock, 0)
//                .orderByDesc(SpuModel::getId);
//        if (dto.getCategoryId() > 0) {
//            //根据第一级分类id,获取第三级分类数据
//            List<MallProductCategoryModel> categoryList = mallProductCategoryMapper.getThirdCategoryByFirstId(dto.getCategoryId(), 0);
//            if (CollUtil.isEmpty(categoryList)) {
//                return new Page(page.getCurrent(), page.getSize(), 0);
//            }
//            List<Long> categoryIdList = categoryList.stream().map(MallProductCategoryModel::getCategoryId).collect(Collectors.toList());
//            queryWrap.in(SpuModel::getCategoryId, categoryIdList);
//        }
//        return this.page(page, queryWrap);
//    }
//
//    @Override
//    public SpuModel getH5Detail(Long id) {
//        SpuModel product = this.getOne(Wrappers.<SpuModel>lambdaQuery()
//                .select(SpuModel::getId, SpuModel::getShopId, SpuModel::getImage, SpuModel::getName, SpuModel::getSliderImage,
//                        SpuModel::getOtPrice, SpuModel::getStock, SpuModel::getSales, SpuModel::getPrice, SpuModel::getIntro,
//                        SpuModel::getFicti, SpuModel::getBrowse, SpuModel::getUnitName, SpuModel::getGuaranteeIds, SpuModel::getBrandId,
//                        SpuModel::getCategoryId)
//                .eq(SpuModel::getId, id)
//                .eq(SpuModel::getIsRecycle, false)
//                .eq(SpuModel::getIsShow, true)
//                .in(SpuModel::getAuditStatus, ProductEnum.AuditEnum.SUCCESS.getCode(), ProductEnum.AuditEnum.NO_AUDIT.getCode())
//        );
//        if (ObjectUtil.isNull(product)) {
//            throw new FailException(StrUtil.format("没有找到ID： {} 的商品", id));
//        }
//        MallProductDescriptionModel sd = mallProductDescriptionMapper.selectOne(Wrappers.<MallProductDescriptionModel>lambdaQuery()
//                .eq(MallProductDescriptionModel::getProductId, product.getId())
//                .eq(MallProductDescriptionModel::getType, ProductEnum.TypeEnum.NORMAL.getType())
//        );
//        if (ObjectUtil.isNotNull(sd)) {
//            product.setContent(StrUtil.isBlank(sd.getDescription()) ? "" : sd.getDescription());
//        }
//        return product;
//    }
//
//    @Override
//    public void addBrowse(Long proId) {
//        this.update(Wrappers.<SpuModel>lambdaUpdate()
//                .setSql("BROWSE = BROWSE + 1")
//                .eq(SpuModel::getId, proId)
//        );
//    }
//
//    @Override
//    public SpuModel getBySpuId(Long spuId) {
//        return this.getById(spuId);
//    }

    private final SpuService spuService;
    private final SkuService skuService;
    private final SpuConverter spuConverter;
    @Override
    public AppSpuVO prodInfo(Long spuId) {
        AppSpuVO spu = spuService.getBySpuId(spuId);
        spu.setSkus(skuService.getSkuBySpuId(spu.getSpuId()));
        return spu;
    }

//    @Override
//    public List<AppProMerchantProductVO> getRecommendedProductsByMerId(Long merId, Long num) {
//        IPage<SpuModel> productPage = this.page(new Page<>(1, num), Wrappers.<SpuModel>lambdaQuery()
//                .select(SpuModel::getId, SpuModel::getShopId, SpuModel::getImage, SpuModel::getName,
//                        SpuModel::getPrice, SpuModel::getSales, SpuModel::getFicti)
//                .eq(SpuModel::getShopId, merId)
//                .eq(SpuModel::getIsRecycle, false)
//                .eq(SpuModel::getIsShow, true)
//                .in(SpuModel::getAuditStatus, ProductEnum.AuditEnum.SUCCESS.getCode(), ProductEnum.AuditEnum.NO_AUDIT.getCode())
//                .orderByDesc(SpuModel::getSortOrder)
//        );
//        List<SpuModel> productList = productPage.getRecords();
//        if (CollUtil.isEmpty(productList)) {
//            return CollUtil.newArrayList();
//        }
//        return productList.stream()
//                .map(product -> BeanUtil.copyProperties(product, AppProMerchantProductVO.class))
//                .collect(Collectors.toList());
//    }
//
//    /**
//     * 通过商户id列表获取商户名map
//     *
//     * @param merIdList
//     * @return
//     */
//    private Map<Long, MallMerchantModel> getMallMerchant(List<Long> merIdList) {
//        Assert.isTrue(CollUtil.isNotEmpty(merIdList), "商户id不能为空");
//        return mallMerchantService.list(Wrappers.<MallMerchantModel>lambdaQuery()
//                .select(MallMerchantModel::getId, MallMerchantModel::getName, MallMerchantModel::getIsSelf, MallMerchantModel::getTypeId, MallMerchantModel::getCategoryId)
//                .in(MallMerchantModel::getId, merIdList)
//        ).stream().collect(Collectors.toMap(MallMerchantModel::getId, Function.identity()));
//    }
}
